#include<bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define se second
#define int long long
using namespace std;
int a[100009];
int b[100009];
string s;
int n,m;
int c[100009];
int l[100009];
int yi[300][5];
int f[100009];
int f1[100009];
signed main(){
    freopen("arena.in","r",stdin);
   freopen("arena.out","w",stdout);

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    l[2]=1;
    int it=2,aa=2;
    for(int i=1;i<=n;++i){
        l[i]=it;
        if(i==aa*2){
            aa*=2;
            it++;
        }

    }

    for(int i=1;i<=n;++i){
        cin>>a[i];
    }
    for(int i=1;i<=m;++i){
        cin>>c[i];
    }
    int flag=0;
    for(int i=1;i<=l[n];++i){
        cin>>s;
        for(int j=0;j<s.size();++j){
            if(s[j]=='1'){
                flag=1;
                break;
            }
        }
    }
    int T;
    cin>>T;
        for(int i=1;i<=T;++i){
        for(int j=0;j<4;++j){
            cin>>yi[i][j];
        }
    }

    for(int z=1;z<=T;++z){

            memset(f,0,sizeof(f));
            memset(f1,0,sizeof(f1));
            for(int j=1;j<=n;++j){
                b[j]=a[j]^yi[z][j%4];

            }

            cout<<flag<<endl;
            if(!flag){
                int sum=0;
                for(int i=1;i<=m;++i){
                    int cnt=0;
                    int N=c[i];
                    int M=1;
                    for(int j=1;j<=l[N];++j){ M*=2;}
                    for(int j=1;j<=N;++j){
                        if(a[j]>=l[M-j+1]){
                            cnt=j;
                            break;
                        }
                    }
                    if(cnt) sum=sum^(i*cnt);
                    else sum=sum^(i*(N+1+M)*(M-N)/2);
                }
            cout<<sum<<"\n";
            }
            else{
                int sum=0;
                for(int i=1;i<=m;++i){
                    sum=sum^(i*i);
                }
            }
        }



    return 0;
}
